package com.mb.cart.controller;

import com.mb.cart.service.CartService;
import com.mb.common.pojo.Cart;
import com.mb.common.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/cart/manage")
public class CartController {

    @Autowired
    private CartService cartService;

    /*
    * 根据用户id查询我的购物车所有商品数据
    * */
    @RequestMapping("/query")
    public List<Cart> queryMycarts(String userId){
        return cartService.queryMyCarts(userId);
    }

    @RequestMapping("/save")
    public SysResult addCart(Cart cart){
        try{
            cartService.addCart(cart);
            return SysResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return SysResult.build(201,"增添商品失败",null);
        }
    }

    @RequestMapping("/update")
    public SysResult gxCartNum(Cart cart){
        try{
            cartService.gxCartNum(cart);
            return SysResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return SysResult.build(201,"更新失败",null);
        }
    }

    @RequestMapping("/delete")
    public SysResult delCart(Cart cart){
        try{
            cartService.delCart(cart);
            return SysResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return SysResult.build(201,"删除失败",null);
        }
    }


}
